﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace DoNet.Common.Utilities
{
    public class StringHelper
    {
        /// <summary>
        /// object转换成Int型
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static int ObjectToInt(object o)
        {
            int result = -1;

            if (null != o)
            {
                int tag = -1;
                bool sign = int.TryParse(o.ToString(), out tag);
                if (sign)
                {
                    result = tag;
                }
            }

            return result;
        }

        /// <summary>
        /// 判断指定字符串是否为数字
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns>是全数字返回true，否则返回false</returns>
        public static bool IsNumber(string str)
        {
            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] < '0' || str[i] > '9')
                    return false;
            } // end for
            return true;
        } // end IsNumber

        /// <summary>
        /// 得到MD5加密的字符串
        /// </summary>
        /// <param name="content">在加密的字符串</param>
        /// <returns></returns>
        public static string GetMD5String(string content)
        {
            byte[] data = System.Text.ASCIIEncoding.ASCII.GetBytes(content);
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] result = md5.ComputeHash(data);
            return System.Convert.ToBase64String(result);

        } // end GetMD5String

        /// <summary>
        /// 编码
        /// </summary>
        /// <param name="codeType">编码类型</param>
        /// <param name="code">待编码字符串</param>
        /// <returns>编码后的字符串</returns>
        public static string EncodeBase64(string codeType, string code)
        {
            string encode = "";
            byte[] bytes = Encoding.GetEncoding(codeType).GetBytes(code);
            try
            {
                encode = Convert.ToBase64String(bytes);
            }
            catch
            {
                encode = code;
            }
            return encode;
        } // end EncodeBase64

        /// <summary>
        /// 解码
        /// </summary>
        /// <param name="codeType">编码类型</param>
        /// <param name="code">待解码字符串</param>
        /// <returns>解码后的字符串</returns>
        public static string DecodeBase64(string codeType, string code)
        {
            string decode = "";
            byte[] bytes = Convert.FromBase64String(code);
            try
            {
                decode = Encoding.GetEncoding(codeType).GetString(bytes);
            }
            catch
            {
                decode = code;
            }
            return decode;
        } // end DecodeBase64

        /// <summary>
        /// 删除前后的空字符
        /// </summary>
        /// <param name="src"></param>
        /// <returns></returns>
        public static string Trim(string src)
        {
            return src.Trim(' ', '\t', '\r', '\n');
        }

        /// <summary>
        /// 删除连续多于1个的空字符(空格回车，换行，tab)
        /// src长度100，平均时间2.9微秒
        /// </summary>
        /// <param name="src"></param>
        /// <returns></returns>
        public static string Suppress(string src)
        {
            if (src == null || src == "")
            {
                return src;
            }

            StringBuilder dest = new StringBuilder();

            //char[] cs = src.ToCharArray();
            bool inBlank = true;

            foreach (char c in src)
            {
                if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
                {
                    inBlank = true;
                }
                else
                {
                    if (inBlank == true)
                    {
                        dest.Append(" ");
                    }
                    inBlank = false;
                    dest.Append(c);
                }
            }

            return dest.ToString(1,dest.Length - 1);
        }
    }
}
